package com.xl.web.controller.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jodd.servlet.URLDecoder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.core.util.HtmlUtil;
import com.xl.core.util.StringUtil;
import com.xl.base.web.BaseController;
import com.xl.web.entity.sys.SysMember;
import com.xl.web.entity.sys.SysMenu;
import com.xl.web.entity.sys.SysRole;
import com.xl.web.service.sys.SysMemberService;
import com.xl.web.service.sys.SysMenuService;
import com.xl.web.service.sys.SysRoleService;

/**
 * 系统后台角色管理控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController {

	// 注入系统角色管理service
	@Autowired(required = false)
	private SysRoleService<SysRole> sysRoleService;

	// 注入系统菜单管理service
	@Autowired(required = false)
	private SysMenuService<SysMenu> sysMenuService;

	// 注入系统用户管理service
	@Autowired(required = false)
	private SysMemberService<SysMember> sysMemberService;

	/**
	 * 角色列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.list")
	@RequestMapping("/list")
	public ModelAndView list(SysRole role) throws Exception {

		Map<String, Object> context = getRootMap(request);
		return forword("sys/role_list", context);
	}
/**
	 * 角色列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {

		SysRole role = (SysRole)getJsonParams(SysRole.class);
		// 查询列表
		List<SysRole> dataList = sysRoleService.queryByList(role);
		sendListData(response, role.getPager().getRowCount(), dataList);
	}
	
	/**
	 * 新增角色页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.handle")
	@Duplicate(add = true)
	@RequestMapping("/add")
	public ModelAndView add() throws Exception {

		Map<String, Object> context = getRootMap(request);
		return forword("sys/role_add", context);
	}

	/**
	 * 修改角色页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.handle")
	@Duplicate(add = true)
	@RequestMapping("/update")
	public ModelAndView update(SysRole role) throws Exception {
		Map<String, Object> context = getRootMap(request);
		// 查询本角色的详细数据
		role = sysRoleService.queryById(role);
		context.put("role", role);
		return forword("sys/role_update", context);
	}

	/**
	 * 分配菜单页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.allotMenu")
	@Duplicate(add = true)
	@RequestMapping("/allotMenu")
	public ModelAndView allotMenu(SysRole role) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询本角色的详细数据
		role = sysRoleService.queryById(role);

		context.put("role", role);
		context.put("jsonData", sysMenuService.getTreeList(role));

		return forword("sys/role_allotMenu", context);
	}

	/**
	 * 角色菜单分配保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.allotMenu")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("allotSave")
	public void allotSave(SysRole role, String checkedMenu) throws Exception {

		if (StringUtil.isEmpty(checkedMenu)) {
			sendFailureMessage(response, "未选中任何菜单");
			return;
		}

		// 保存
		sysRoleService.allotSave(role, checkedMenu);

		sendSuccessMessage(response, "分配成功");
	}

	/**
	 * 分配人员页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.allotMember")
	@Duplicate(add = true)
	@RequestMapping("/allotMember")
	public ModelAndView allotMember(SysRole role) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询本角色的详细数据
		role = sysRoleService.queryById(role);
		context.put("role", role);

		// 查询已分配此角色的用户
		List<SysMember> selectedMem = sysMemberService.selectByRoleId(role
				.getId());
		context.put("selectedMem", selectedMem);

		// 全部的用户
		SysMember mem = new SysMember();
		mem.getPager().setLimitDirection(false);
		List<SysMember> allMem = sysMemberService.queryByList(mem);

		// 过滤掉已经选中的用户
		List<String> selectedIdList = new ArrayList<String>();
		for (SysMember item : selectedMem) {
			selectedIdList.add(item.getId());
		}
		for (int i = allMem.size() - 1; i >= 0; i--) {
			SysMember item = allMem.get(i);
			if (selectedIdList.contains(item.getId())) {
				allMem.remove(i);
			}
		}
		context.put("allMem", allMem);

		return forword("sys/role_allotMember", context);
	}

	/**
	 * 角色菜单分配保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.allotMember")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("allotMemberSave")
	public void allotMemberSave(SysRole role, String checkedMember)
			throws Exception {

		if (checkedMember == null) {
			sendFailureMessage(response, "未选中任何人员");
			return;
		}

		// 保存
		sysRoleService.allotMemberSave(role, checkedMember);

		sendSuccessMessage(response, "分配成功");
	}

	/**
	 * 新增角色保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addSave")
	public void addSave(SysRole role) throws Exception {

		// 新增角色保存
		if (StringUtil.isEmpty(role.getId())) {
			sysRoleService.add(role);
		} else {
			// 更新角色保存
			sysRoleService.updateBySelective(role);
		}

		sendSuccessMessage(response, "添加成功");
	}

	/**
	 * 删除角色保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.handle")
	@ResponseBody
	@RequestMapping("deleteRole")
	public void deleteRole(SysRole role) throws Exception {

		// 逻辑删除本条数据
		sysRoleService.delete(role.getId());

		sendSuccessMessage(response, "删除成功");
	}

	/**
	 * 验证角色编码或者角色名称是否已经存在
	 * 
	 * @param request
	 * @param responce
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.role.list")
	@ResponseBody
	@RequestMapping("/checkRoleExist")
	public void checkMemName(SysRole role) throws Exception {

		// 参数转换，防止中文乱码
		if (!StringUtil.isEmpty(role.getRoleCode())) {
			role.setRoleCode(URLDecoder.decode(URLDecoder.decode(role
					.getRoleCode())));
		}
		if (!StringUtil.isEmpty(role.getRoleName())) {
			role.setRoleName(URLDecoder.decode(URLDecoder.decode(role
					.getRoleName())));
		}

		// 根据用户名获取用户数据
		int size = sysRoleService.checkRoleExist(role);
		// 如果用户不存在，返回true
		if (size > 0) {
			HtmlUtil.writerJson(response, false);
		} else {
			HtmlUtil.writerJson(response, true);
		}
	}
}
